home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / newmat03.lha / newmat03 / svd.cxx < prev   
C/C++ Source or Header  |  1993-08-08  |  4KB  |  159 lines

  1. //$$svd.cxx                           singular value decomposition
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #define WANT_MATH
  6.  
  7. #include "include.hxx"
  8. #include "newmat.hxx"
  9. #include "newmatrm.hxx"
  10. #include "precisio.hxx"
  11.  
  12.  
  13. void SVD(const Matrix& A, DiagonalMatrix& Q, Matrix& U, Matrix& V,
  14.    BOOL withU, BOOL withV)
  15. // from Wilkinson and Reinsch: "Handbook of Automatic Computation"
  16. {
  17.    real eps = FloatingPointPrecision::Epsilon();
  18.    real tol = FloatingPointPrecision::Minimum()/eps;
  19.  
  20.    int m = A.Nrows(); int n = A.Ncols();
  21.    if (m<n) MatrixError("Matrix must have nRows >= nCols in SVD");
  22.  
  23.    U = A.c(); real g = 0.0; real f,h; real x = 0.0;
  24.    RowVector E(n); RectMatrixRow EI(E,0); Q.ReDimension(n);
  25.    RectMatrixCol UCI(U,0); RectMatrixRow URI(U,0,1,n-1);
  26.  
  27.    for (int i=0; i<n; i++)
  28.    {
  29.       EI.First() = g; real ei = g; EI.Right(); real s = UCI.SumSquare();
  30.       if (s<tol) Q.element(i) = 0.0;
  31.       else
  32.       {
  33.      f = UCI.First(); g = -sign(sqrt(s), f); h = f*g-s; UCI.First() = f-g;
  34.      Q.element(i) = g; RectMatrixCol UCJ = UCI; int j=n-i;
  35.      while (--j) { UCJ.Right(); UCJ.AddScaled(UCI, (UCI*UCJ)/h); }
  36.       }
  37.  
  38.       s = URI.SumSquare();
  39.       if (s<tol) g = 0.0;
  40.       else
  41.       {
  42.      f = URI.First(); g = -sign(sqrt(s), f); URI.First() = f-g;
  43.      EI.Divide(URI,f*g-s); RectMatrixRow URJ = URI; int j=m-i;
  44.      while (--j) { URJ.Down(); URJ.AddScaled(EI, URI*URJ); }
  45.       }
  46.  
  47.       real y = fabs(Q.element(i)) + fabs(ei); if (x<y) x = y;
  48.       UCI.DownDiag(); URI.DownDiag();
  49.    }
  50.  
  51.    if (withV)
  52.    {
  53.       V.ReDimension(n,n); V = 0.0; RectMatrixCol VCI(V,n,n,0);
  54.       for (i=n-1; i>=0; i--)
  55.       {
  56.      URI.UpDiag(); VCI.Left();
  57.      if (g!=0.0)
  58.      {
  59.         VCI.Divide(URI, URI.First()*g); int j = n-i;
  60.         RectMatrixCol VCJ = VCI;
  61.         while (--j) { VCJ.Right(); VCJ.AddScaled( VCI, (URI*VCJ) ); }
  62.      }
  63.      VCI.Zero(); VCI.Up(); VCI.First() = 1.0; g=E.element(i);
  64.       }
  65.    }
  66.  
  67.    if (withU)
  68.    {
  69.       for (i=n-1; i>=0; i--)
  70.       {
  71.      UCI.UpDiag(); g = Q.element(i); URI.Reset(U,i,i+1,n-i-1); URI.Zero();
  72.          if (g!=0.0)
  73.          {
  74.         h=UCI.First()*g; int j=n-i; RectMatrixCol UCJ = UCI;
  75.         while (--j)
  76.             {
  77.            UCJ.Right(); UCI.Down(); UCJ.Down(); real s = UCI*UCJ;
  78.                UCI.Up(); UCJ.Up(); UCJ.AddScaled(UCI,s/h);
  79.             }
  80.         UCI.Divide(g);
  81.          }
  82.          else UCI.Zero();
  83.          UCI.First() += 1.0;
  84.       }
  85.    }
  86.  
  87.    eps *= x;
  88.    for (int k=n-1; k>=0; k--)
  89.    {
  90.       real z; real y; int limit = 50; int l;
  91.       while (limit--)
  92.       {
  93.      real c=0.0; real s=1.0; int i; int l1=k; BOOL tfc=FALSE;
  94.      for (l=k; l>=0; l--)
  95.      {
  96. //          if (fabs(E.element(l))<=eps) goto test_f_convergence;
  97.         if (fabs(E.element(l))<=eps) { tfc=TRUE; break; }
  98.         if (fabs(Q.element(l-1))<=eps) { l1=l; break; }
  99.      }
  100.          if (!tfc)
  101.          {
  102.         l=l1; l1=l-1;
  103.         for (i=l; i<=k; i++)
  104.         {
  105.            f = s * E.element(i); E.element(i) *= c;
  106. //             if (fabs(f)<=eps) goto test_f_convergence;
  107.            if (fabs(f)<=eps) break;
  108.            g = Q.element(i); h = sqrt(f*f + g*g); Q.element(i) = h;
  109.            if (withU)
  110.            {
  111.               RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,l1);
  112.               ComplexScale(UCI, UCJ, g/h, -f/h);
  113.                }
  114.             }
  115.      }
  116. //    test_f_convergence: z = Q.element(k); if (l==k) goto convergence;
  117.      z = Q.element(k);  if (l==k) break;
  118.  
  119.      x = Q.element(l); y = Q.element(k-1);
  120.      g = E.element(k-1); h = E.element(k);
  121.      f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y); g = sqrt(f*f + 1);
  122.      f = ((x-z)*(x+z) + h*(y / ((f<0.0) ? f-g : f+g)-h)) / x;
  123.  
  124.      c = 1.0; s = 1.0;
  125.      for (i=l+1; i<=k; i++)
  126.      {
  127.         g = E.element(i); y = Q.element(i); h = s*g; g *= c;
  128.         z = sqrt(f*f + h*h); E.element(i-1) = z; c = f/z; s = h/z;
  129.         f = x*c + g*s; g = -x*s + g*c; h = y*s; y *= c;
  130.         if (withV)
  131.         {
  132.            RectMatrixCol VCI(V,i); RectMatrixCol VCJ(V,i-1);
  133.            ComplexScale(VCI, VCJ, c, s);
  134.         }
  135.         z = sqrt(f*f + h*h); Q.element(i-1) = z; c = f/z; s = h/z;
  136.         f = c*g + s*y; x = -s*g + c*y;
  137.         if (withU)
  138.         {
  139.            RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,i-1);
  140.            ComplexScale(UCI, UCJ, c, s);
  141.         }
  142.      }
  143.      E.element(l) = 0.0; E.element(k) = f; Q.element(k) = x;
  144.       }
  145.       if (l!=k) MatrixError("SVD convergence fails");
  146. // convergence:
  147.       if (z < 0.0)
  148.       {
  149.      Q.element(k) = -z;
  150.      if (withV) { RectMatrixCol VCI(V,k); VCI.Negate(); }
  151.       }
  152.    }
  153. }
  154.  
  155. void SVD(const Matrix& A, DiagonalMatrix& D)
  156.    { Matrix U; SVD(A, D, U, U, FALSE, FALSE); }
  157.  
  158.  
  159.